package com.anosi.asset.util;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

public class PinyinUtil {

	private static HanyuPinyinOutputFormat format = null;  
    static {  
        format = new HanyuPinyinOutputFormat();  
        //拼音小写  
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        //无音标方式；WITH_TONE_NUMBER：1-4数字表示英标；WITH_TONE_MARK：直接用音标符（必须WITH_U_UNICODE否则异常  
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        //用v表示ü  
        format.setVCharType(HanyuPinyinVCharType.WITH_V);  
    }  
      
    /** 
     * @param str 
     * @return 
     * @Author:jinyao  
     * @Description: 返回字符串的拼音 
     */  
    public static String[] getStringPinYin(String str) {  
        if (str == null || str.length() < 1) {  
            return null;  
        }  
        List<String> result = new ArrayList<String>();  
        //对字符串中的记录逐个分析  
        for (int i = 0; i < str.length(); i++) {  
            result = getStringPinYin(str.charAt(i), result);  
        }  
        return result.toArray(new String[result.size()]);  
    }  
      
    /** 
     * @param c 
     * @param list 
     * @return 
     * @Author:jinyao  
     * @Description: 将字符c的拼音拼接到list中的记录中 
     */  
    private static List<String> getStringPinYin(char c, List<String> list) {  
        String[] strs = getCharPinYin(c);  
        List<String> result = new ArrayList<String>();  
        //如果解析出的拼音为空，判断字符C是否为英文字母，如果是英文字母则添加值拼音结果中  
        if (strs == null) {  
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {  
                c = c <= 91 ? (char)(c + 32) : c;  
                if (list == null || list.size() == 0) {  
                    result.add(c + "");  
                } else {  
                    for (String s : list) {  
                        result.add(s + c);  
                    }  
                }  
                return result;  
            }  
            return list;  
        }  
        //将字符C的拼音首和已存在的拼音首组合成新的记录  
        for (String str : strs) {  
            if (list == null || list.size() == 0) {  
                result.add(str);  
            } else {  
                for (String s : list) {  
                    result.add(s + str);  
                }  
            }  
        }  
        return result;  
    }  
      
    /** 
     * @param c 
     * @return 
     * @Author:jinyao  
     * @Description: 返回汉字的拼音 
     */  
    public static String[] getCharPinYin(char c) {  
        try {  
            //返回字符C的拼音  
            return PinyinHelper.toHanyuPinyinStringArray(c, format);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
      
    /** 
     * @param str 
     * @return 
     * @Author:jinyao  
     * @Description: 返回字符串的拼音的首字母 
     */  
    public static String[] getStringPinYinPrefix(String str) {  
        if (str == null || str.length() < 1) {  
            return null;  
        }  
        List<String> result = new ArrayList<String>();  
        //对字符串中的记录逐个分析  
        for (int i = 0; i < str.length(); i++) {  
            result = getCharPinYinChar(str.charAt(i), result);  
        }  
        return result.toArray(new String[result.size()]);  
    }  
      
    /** 
     * @param c 
     * @param list 
     * @return 
     * @Author:jinyao  
     * @Description: 将字符c的拼音首字母拼接到list中的记录中 
     */  
    private static List<String> getCharPinYinChar(char c, List<String> list) {  
        char[] chars = getCharPinYinPrefix(c);  
        List<String> result = new ArrayList<String>();  
        //如果解析出的拼音为空，判断字符C是否为英文字母，如果是英文字母则添加值拼音结果中  
        if (chars == null) {  
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {  
                c = c < 91 ? (char)(c + 32) : c;  
                if (list == null || list.size() == 0) {  
                    result.add(c + "");  
                } else {  
                    for (String s : list) {  
                        result.add(s + c);  
                    }  
                }  
                return result;  
            }  
            return list;  
        }  
        //将字符C的拼音首字母和已存在的拼音首字母组合成新的记录  
        for (char ch : chars) {  
            if (list == null || list.size() == 0) {  
                result.add(ch + "");  
            } else {  
                for (String s : list) {  
                    result.add(s + ch);  
                }  
            }  
        }  
        return result;  
    }  
      
    /** 
     * @param c 
     * @return 
     * @Author:jinyao  
     * @Description:返回汉字拼音首字母 
     */  
    public static char[] getCharPinYinPrefix(char c) {  
        //字符C的拼音  
        String[] strs = getCharPinYin(c);  
        if (strs != null) {  
            //截取拼音的首字母  
            char[] chars = new char[strs.length];  
            for(int i = 0; i <chars.length; i++) {  
                chars[i] = strs[i].charAt(0);  
            }  
            return chars;  
        }  
        return null;  
    }  
	
}
